package net.sf.odinms.exttools.doclet.pages;

import java.io.IOException;
import java.util.List;
import java.util.Vector;
import net.sf.odinms.exttools.doclet.Doclet;

/**
 * Creates "index-files/index-#.html" for each letter # with symbols.
 */
public final class SplitIndex extends AbstractIndex {
	
	/** The first letter for symbols on the page being generated. */
	private static Character curLetter;
	/** The letter for the preceding index page, if one exists. */
	private static Character prevLetter;
	/** The letter for the following index page, if one exists. */
	private static Character nextLetter;
	
	/** Localized text for "Previous Letter". */
	private static String prevLabel = CONF.propertyText("PrevLetter");
	/** Localized text for "Next Letter". */
	private static String nextLabel = CONF.propertyText("NextLetter");
	
	/**
	 * Generate index pages for each letter with symbols in the Javadocs.
	 */
	public static void generateSplitIndexPages() {
		String filename = null;
		String pathToFile = "index-files" + SLASH;
		
		try {
			Object[] indexLetters = CONF.indexBuilder.elements();
			System.out.println(CONF.propertyText("doclet.index"));
			for (int i = 0; i < indexLetters.length; i++) {
				prevLetter = curLetter;
				curLetter = (Character) indexLetters[i];
				nextLetter =
						(i + 1 < indexLetters.length)
								? (Character) indexLetters[i + 1]
								: null;
				filename = "index-" + curLetter + CONF.ext;
				new SplitIndex(filename, pathToFile);
			}
			
		}
		catch (IOException exc) {
			throw Doclet.exception(exc, filename);
		}
	}
	
	/**
	 * Initialize and create an index page for the specified starting character.
	 * Calls {@link #generateCharacterList} with the letter for the current
	 * index page.
	 * 
	 * @param filename The desired name of the file (with extension).
	 * @param pathToFile The path from the document root to this file.
	 * @throws IOException If {@link java.io.FileOutputStream} creation fails.
	 */
	private SplitIndex(String filename, String pathToFile) throws IOException {
		super(filename, pathToFile);
		
		windowTitle =
				(CONF.windowtitle.length() > 0)
						? CONF.windowtitle
						: CONF.propertyText("Index", curLetter.toString());
		
		printXhtmlHeader();
		characterList(curLetter, CONF.indexBuilder.getMemberList(curLetter));
		
		printXhtmlFooter();
		this.close();
	}
	
	/**
	 * Print links to previous and next index pages (if they exist).
	 */
	protected void navPreviousNext() {
		println(openULWithID("PreviousNext"));
		String prevLink =
				(prevLetter == null) ? prevLabel : linkToLabelHref(prevLabel,
						"index-" + prevLetter + CONF.ext);
		String nextLink =
				(nextLetter == null) ? nextLabel : linkToLabelHref(nextLabel,
						"index-" + nextLetter + CONF.ext);
		println(listItem(prevLink) + listItemLast(nextLink));
		println(close("ul"));
		
	}
	
	/** Creates links to index pages for each letter with documented symbols. */
	protected void navWithinPage() {
		List<String> letters = new Vector<String>();
		String letterLink;
		for (Object letter : CONF.indexBuilder.elements()) {
			letterLink =
					(curLetter.equals(letter))
							? letter.toString()
							: linkToLabelHref(letter.toString(), "index-"
									+ letter + CONF.ext);
			letters.add(letterLink);
		}
		print(open("td id=\"WithinPage\""));
		printUnorderedListWithLast(letters);
		println(close("td"));
	}
	
}
